package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.*;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int sum = 0;
        for (int i = leftBorder; i <= rightBorder; i++) {
            if (i%2 == 0){
                sum = i+sum;
            }
        }
        return sum;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int sum = 0;
        for (int i = leftBorder; i <= rightBorder; i++) {
            if ((i-1)%2 == 0){
                sum = i+sum;
            }
        }
        return sum;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int sum = 0;
        for (Integer i : arrayList) {
            int v = 1;
            v++;
            sum = i*3+2+sum ;

        }
        return sum;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        for (int i = 0; i < arrayList.toArray().length; i++) {
            int k = arrayList.get(i);
            if (k%2 != 0){
                k = k * 3 + 2;
                arrayList.set(i, k);
            }
        }
        return arrayList ;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        int sum = 0;
        for (Integer i : arrayList) {
            if (i%2 != 0){
                sum = i*3+5 + sum;
            }

        }
        return sum;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        ArrayList<Integer> list = new ArrayList<>();
        Integer sum = 0;
        for (int i = 0; i <arrayList.toArray().length -1; i++) {
            Integer k = arrayList.get(i);
            Integer m = arrayList.get(i + 1);
            Integer a = (k+m)*3;
            list.add(a);
        }
        return list;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        int length = arrayList.toArray().length;
        Integer k = 0;
        Integer m = 0;
        Integer a =0;
        if (length%2==0){
             k = arrayList.get(length / 2 + 1);
             m = arrayList.get(length / 2 - 1);
             a = (k + m)/2;

        }else {
            a = arrayList.get((length -1) /2);
        }
        return a;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        int length = arrayList.toArray().length;
        Integer k = 0;
        Integer m = 0;
        Integer a = 0;
        for (Integer value:arrayList) {
            if (value%2==0){
                a = value + a;
                k++;
            }
        }
        return a/k;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        int length = arrayList.toArray().length;
        ArrayList<Integer> list = new ArrayList<>();
        Integer k = 0;
        Integer m = 0;
        Integer a = 0;
        for (Integer value:arrayList) {
            if (value%2==0){
                list.add(value);
            }
        }
        return list.contains(specialElment);
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer value:arrayList) {
            if (value % 2 == 0) {
                list.add(value);
            }
            int length = list.size();
            for (int i = 0; i <length-1 ; i++) {
            if (list.get(i) == list.get(i+1)){
                   list.remove(i);
               }
            }

        }
        return list;
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        int length = arrayList.toArray().length;
        ArrayList<Integer> list = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        ArrayList<Integer> list3 = new ArrayList<>();
        Integer k = 0;
        Integer m = 0;
        Integer a = 0;
        for (int i = 0; i < length ; i++) {
            if (arrayList.get(i)%2==0){
                list.add(arrayList.get(i));
                list.sort(Comparator.naturalOrder());
            }
            else {
                list2.add(arrayList.get(i));
                list2.sort(Comparator.reverseOrder());
            }

        }

        list3.addAll(list);
        list3.addAll(list2);
        return list3;
    }
}
